/*
 * @(#)ConnectorEdge.java   09/12/30
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.editor.ass;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.ContentController;
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.julia.control.binding.Util;

import fr.ema.arch3d.ref.Fractal;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class ConnectorEdge extends MultiEdge {

    /** Field description */
    Object[] connector = new Object[4];

    /** Field description */
    Component cmpR, cmpP;

    /** Field description */
    Component cmpR1, cmpP1;

    /** Field description */
    String requiredItf, providedItf;

    /** Field description */
    String requiredItf1, providedItf1;

    /** Field description */
    String name;

    /**
     * Constructs ...
     *
     *
     * @param connector
     */
    public ConnectorEdge(Component connector) {
        try {
            name = Fractal.getNameController(connector).getFcName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Interface itfPctr;

        try {
            ComponentType it=(ComponentType) connector.getFcType();
            String ritf=null, pitf=null;
            for(int s=0;s<it.getFcInterfaceTypes().length;s++){
        	if(it.getFcInterfaceTypes()[s].isFcClientItf())
        	    ritf=it.getFcInterfaceTypes()[s].getFcItfName();
            }
            for(int s=0;s<it.getFcInterfaceTypes().length;s++){
        	if(!it.getFcInterfaceTypes()[s].isFcClientItf())
        	    pitf=it.getFcInterfaceTypes()[s].getFcItfName();
            }
            Interface itfR1  = (Interface) Fractal.getBindingController(connector).lookupFc(ritf);
            Interface itfR2 = (Interface) Fractal.getBindingController(connector).lookupFc(ritf);
          
            itfPctr = (Interface) connector.getFcInterface(pitf);
            Object[] comps=Util.getFcPotentialClientsOf(itfPctr).toArray();
            Component a=null;
            for(int i=0;i<comps.length;i++){
        	System.out.println(org.objectweb.fractal.util.Fractal.getNameController((Component)comps[i]).getFcName());
            if(org.objectweb.fractal.util.Fractal.getNameController((Component)comps[i]).getFcName().equals("BikeTripC1"))
            a=(Component) comps[i];
            }
            
            //Component b=((Interface) a.getFcInterface("BikeQSR")).getFcItfOwner();
            //System.out.println("8888888888="+Fractal.getNameController(b).getFcName());
            System.out.println(itfPctr.getFcItfName()+"====="+getFcClientItfsBoundTo((Component)comps[1],itfPctr));
            Object[] itfRs = Util.getFcClientItfsBoundTo(itfPctr).toArray();

            this.cmpP = itfR1.getFcItfOwner();
            System.out.println("8888888888="+Fractal.getNameController(this.cmpP).getFcName());
            
            this.cmpR = ((Interface) itfRs[0]).getFcItfOwner();

            for (int i = 0; i < itfRs.length; i++) {
                Component t = ((Interface) itfRs[i]).getFcItfOwner();

                // System.out.println(name+"="+"here==="+Fractal.getNameController(t).getFcName());
            }

            if (itfRs.length > 1) {
                this.cmpR1 = ((Interface) itfRs[1]).getFcItfOwner();

                // System.out.println("here==="+Fractal.getNameController(cmpR1).getFcName());
                this.requiredItf1 = ((Interface) itfRs[1]).getFcItfName();
            }

            if (itfR2 != null) {
                this.cmpP1         = itfR2.getFcItfOwner();
                this.providedItf1 = itfR2.getFcItfName();
            }

            this.requiredItf = ((Interface) itfRs[0]).getFcItfName();
            this.providedItf = itfR1.getFcItfName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // System.out.println(cmpP1+"="+cmpR1);
    }

    /**
     * Constructs ...
     *
     *
     * @param connector
     * @param newCmpName
     */
    public ConnectorEdge(Component connector, String newCmpName) {
        try {
            name = Fractal.getNameController(connector).getFcName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Interface pctr;

        try {
            Interface pcmp  = (Interface) Fractal.getBindingController(connector).lookupFc("r-I");
            Interface pcmp1 = (Interface) Fractal.getBindingController(connector).lookupFc("r-II");

            // System.out.println(name+"="+pcmp1.getFcItfName());
            pctr = (Interface) connector.getFcInterface("p");

            Object[] rcmp = Util.getFcClientItfsBoundTo(pctr).toArray();

            this.cmpP = pcmp.getFcItfOwner();

            if (rcmp.length == 1) {
                this.cmpR = ((Interface) rcmp[0]).getFcItfOwner();
            } else {
                if (!Fractal.getNameController(((Interface) rcmp[0]).getFcItfOwner()).getFcName().equals(newCmpName)) {
                    this.cmpR          = ((Interface) rcmp[0]).getFcItfOwner();
                    this.cmpR1         = ((Interface) rcmp[1]).getFcItfOwner();
                    this.requiredItf1 = ((Interface) rcmp[1]).getFcItfName();
                } else {
                    this.cmpR          = ((Interface) rcmp[1]).getFcItfOwner();
                    this.cmpR1         = ((Interface) rcmp[0]).getFcItfOwner();
                    this.requiredItf1 = ((Interface) rcmp[0]).getFcItfName();
                }
            }

            if (pcmp1 != null) {
                this.cmpP1         = pcmp1.getFcItfOwner();
                this.providedItf1 = pcmp1.getFcItfName();
            }

            this.requiredItf = ((Interface) rcmp[0]).getFcItfName();
            this.providedItf = pcmp.getFcItfName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // System.out.println(cmpP1+"="+cmpR1);
    }

    /**
     * Method description
     *
     * @param cmpR  
     * @param requiredItf  
     * @param cs  
     * @param providedItf  
     */
    void setConnection(Component cc, String client_itf, Component cs, String server_itf) {}

    /**
     * Method description
     *
     * @return  
     */
    Object[] getConnection() {
        connector[0] = this.cmpR;
        connector[1] = this.requiredItf;
        connector[2] = this.cmpP;
        connector[3] = this.providedItf;

        return connector;
    }
    
    public static List getFcClientItfsBoundTo (
	    final Component component,
	    final Interface serverItf) throws Exception
	  {
	    List itfList = new ArrayList();
	    BindingController bc;
	    try {
	      bc = (BindingController)component.getFcInterface("binding-controller");
	    } catch (NoSuchInterfaceException e) {
	      return itfList;
	    }

	    // search in external client interfaces
	    Object[] itfs = component.getFcInterfaces();
	    for (int i = 0; i < itfs.length; ++i) {
	      Interface itf;
	      InterfaceType itfType;
	      try {
	        itf = (Interface)itfs[i];
	        itfType = (InterfaceType)itf.getFcItfType();
	      } catch (ClassCastException e) {
	        throw new Exception("Cannot create shortcuts");
	      }
	      if (!itfType.isFcClientItf()) {
	        continue;
	      }
	      Object sItf;
	      try {
	        sItf = bc.lookupFc(itf.getFcItfName());
	      } catch (NoSuchInterfaceException e) {
	        continue;
	      }
	      if (serverItf.equals(sItf)) {
	        itfList.add(itf);
	      }
	    }

	    ContentController cc;
	    try {
	      cc = (ContentController)component.getFcInterface("content-controller");
	    } catch (NoSuchInterfaceException e) {
	      return itfList;
	    }

	    // search in internal client interfaces
	    itfs = cc.getFcInternalInterfaces();
	    for (int i = 0; i < itfs.length; ++i) {
	      Interface itf;
	      InterfaceType itfType;
	      try {
	        itf = (Interface)itfs[i];
	        itfType = (InterfaceType)itf.getFcItfType();
	      } catch (ClassCastException e) {
	        throw new Exception("Cannot create shortcuts");
	      }
	      if (!itfType.isFcClientItf()) {
	        continue;
	      }
	      Object sItf;
	      try {
	        sItf = bc.lookupFc(itf.getFcItfName());
	      } catch (NoSuchInterfaceException e) {
	        continue;
	      }
	      if (serverItf.equals(sItf)) {
	        itfList.add(itf);
	      }
	    }
	    return itfList;
	  }

}
